// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Plane.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Robotics.Geometry
{
    using System;
    using System.Runtime.Serialization;

    using Microsoft.Robotics.Numerics;

    /// <summary>
    /// Represents a plane
    /// </summary>
    [DataContract]
    public class Plane
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Plane"/> class from 3 points on the plane.
        /// Points must be distinct from each other.
        /// </summary>
        /// <param name="p1">A point on the plane.</param>
        /// <param name="p2">A second point on the plane.</param>
        /// <param name="p3">A third point on the plane.</param>
        public Plane(Vector3 p1, Vector3 p2, Vector3 p3)
        {
            if (p1 == p2 || p2 == p3 || p1 == p3)
            {
                throw new ArgumentException("p1, p2, and p3 must be distinct");
            }

            Vector3 v1 = p1 - p2;
            Vector3 v2 = p1 - p3;

            Vector3 zeroVector = new Vector3(0, 0, 0);
            Vector3 n = Vector3.Cross(v1, v2);
            if (n == zeroVector)
            {
                throw new ArgumentException("p1, p2, and p3 must not be colinear");
            }

            n = Vector3.Normalize(n);
            this.Normal = n;

            // Get the point closes to the origin to the plane.
            this.Point = zeroVector - Vector3.Dot(zeroVector - p1, n) * n;
        }

        /// <summary>
        /// Gets the normal to the plane
        /// </summary>
        [DataMember]
        public Vector3 Normal { get; private set; }

        /// <summary>
        /// Gets the point on the plane closest to the origin.
        /// </summary>
        [DataMember]
        public Vector3 Point { get; private set; }

        /// <summary>
        /// Project a point onto the plane
        /// </summary>
        /// <param name="point">Point to project onto the plane</param>
        /// <returns>Projection of point onto the plane.</returns>
        public Vector3 ProjectPointToPlane(Vector3 point)
        {
            return point - (Vector3.Dot(point - this.Point, this.Normal) * this.Normal);
        }
    }
}
